<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8" :hidden="true">
                <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle">
                  <a-input
                    :disabled="formDisabled"
                    v-model="model.bpmBizTitle"
                    placeholder="请输入标题"
                    :max-length="255"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
                  <a-input
                    v-model="model.billNo"
                    placeholder="请输入单据编号"
                    disabled
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposer">
                  <select-user-by-dep
                    :multi="false"
                    v-model="model.proposer"
                    disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
                  <j-date
                    placeholder="请选择申请时间"
                    v-model="model.createTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm:ss"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposerDeptId">
                  <j-select-depart :disabled="formDisabled || showFlowSubmitButton" v-model="model.proposerDeptId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applicant">
                  <select-Company-input :disabled="formDisabled || showFlowSubmitButton" v-model="model.applicant" @change="applicantChange"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billCategory">
                  <j-dict-select-tag :disabled="formDisabled || showFlowSubmitButton" type="list" v-model="model.billCategory" dictCode="bill_category" placeholder="请选择单据类型" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="费用类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feeCategory">
                  <j-dict-select-tag
                    :disabled="formDisabled || showFlowSubmitButton"
                    type="list"
                    v-model="model.feeCategory"
                    dictCode="fee_category_project"
                    placeholder="请选择费用类型"
                    @input="radioChange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="项目承接人"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="projectUndertaker"
                >
                  <select-user-by-dep
                    :multi="false"
                    v-model="model.projectUndertaker"
                    disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>

              <a-col :span="8">
                <a-form-model-item label="保证金所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input
                    v-model="model.teamId"
                    :tdlx="3"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" :hidden="model.feeCategory!='2' && model.feeCategory!='4' && model.feeCategory!='5' ">
                <a-form-model-item
                  label="关联项目"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="preProjectId"
                >
                  <a-input
                    v-if="formDisabled || showFlowSubmitButton"
                    readOnly
                    @click.prevent="goProjectDetail(model)"
                    v-model="model.preProjectId_dictText"
                    style="color: #1890FF; cursor: pointer;"
                    placeholder="请选择项目"></a-input>
                  <select-project-input
                    v-else
                    :key="timer"
                    :signUnit="model.applicant"
                    v-model="model.preProjectId"
                    placeholder="请输入选择项目"
                    :costType="model.feeCategory"
                    :disabled="formDisabled || !model.feeCategory || !model.applicant  || showFlowSubmitButton"
                    @input="selectProject" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectCode">
                  <a-input disabled v-model="model.projectCode" placeholder="请输入项目编号" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="业绩归属年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attributionYear">
                  <a-input disabled v-model="model.attributionYear" placeholder="请选择业绩归属年度" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24" >
                <a-form-model-item
                  label="关联投标档案"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="tenderId"
                >
                  <a-input
                    v-if="(formDisabled || showFlowSubmitButton) && model.tenderId"
                    readOnly
                    @click.prevent="goTenderDetail(model)"
                    v-model="model.preProjectId_dictText"
                    style="color: #1890FF; cursor: pointer;"
                    placeholder="请选择投标档案"></a-input>
                  <a-input v-else-if="formDisabled && !model.tenderId" readOnly style="color: #1890FF; cursor: pointer;" placeholder="请选择投标档案"></a-input>
                  <select-omc-project-tender-input-for-deposit-bill
                    v-else
                    :disabled="model.feeCategory=='1'|| this.model.feeCategory == '3' ?(formDisabled || !model.feeCategory || !model.applicant  || showFlowSubmitButton):(formDisabled || !model.feeCategory || !model.applicant || !model.preProjectId  || showFlowSubmitButton)"
                    :tenderUnit="model.applicant"
                    :projectId="model.feeCategory=='1'|| this.model.feeCategory == '3'?'':model.preProjectId"
                    v-model="model.tenderId"
                    :isResult="model.feeCategory=='1'|| this.model.feeCategory == '3'?true:false"
                    :isBid="model.feeCategory=='2'||this.model.feeCategory == '4'||this.model.feeCategory == '5' ?1:0"
                    @back="popupCallback" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="打款信息">
            <a-row :gutter="48">
              <a-col :span="8" v-if="model.feeCategory=='2'">
                <a-form-model-item label="是否投标转履约" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRelDeposit">
                  <j-dict-select-tag  @input="isRelDeposit" :disabled="formDisabled  || showFlowSubmitButton" type="list" v-model="model.isRelDeposit" dictCode="yes_or_no" placeholder="请选择是否投标转履约" />
                </a-form-model-item>
              </a-col>
            <a-col :span="8" v-if="model.feeCategory=='2' && model.isRelDeposit=='1'">
                <a-form-model-item label="关联投标保证金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relDepositBill">
                  <select-finance-deposit-bill-input
                          :disabled="formDisabled || !model.preProjectId || showFlowSubmitButton"
                          v-model="model.relDepositBill"
                          :projectId="model.preProjectId"
                          :feeCategory="'1'"
                          @back="relBillData"
                          placeholder="请选择关联单据单号" />
                </a-form-model-item>
            </a-col>
            <a-col :span="8" v-if="model.feeCategory=='2' && model.isRelDeposit=='1'">
                <a-form-model-item
                  label="原投标保证金金额（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="originPaymentAmount"
                >
                  <a-input-number
                    :disabled="formDisabled || showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    v-model="model.originPaymentAmount"
                    placeholder="请输入原投标保证金金额（元）"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="汇款单位名称"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="remittanceUnit"
                >
                  <select-Company-input :disabled="true" v-model="model.remittanceUnit" placeholder="请选择汇款单位"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="汇款截止时间"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="remittanceEndTime"
                >
                  <j-date
                    :disabled="formDisabled || showFlowSubmitButton"
                    placeholder="请选择汇款截止时间"
                    v-model="model.remittanceEndTime"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="汇款账户要求"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="remittanceAccountRequirements"
                >
                <j-dict-select-tag
                    :disabled="formDisabled || showFlowSubmitButton"
                    type="list"
                    v-model="model.remittanceAccountRequirements"
                    dictCode="remittance_account_requirements"
                    placeholder="请输入汇款账户要求"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="汇款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remittanceMode">
                  <j-dict-select-tag
                    :disabled="formDisabled || showFlowSubmitButton"
                    type="list"
                    v-model="model.remittanceMode"
                    dictCode="remittance_mode"
                    placeholder="请选择汇款方式"
                    @input="radioModeChange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.feeCategory=='3' || model.feeCategory=='4' || model.feeCategory=='5'">
                <a-form-model-item
                  label="保函金额（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="letterAmount"
                >
                  <a-input-number
                    :disabled="formDisabled || showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    v-model="model.letterAmount"
                    placeholder="请输入保函金额"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="保证金金额（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="remittanceAmount"
                >
                  <a-input-number
                    :disabled="formDisabled || showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    v-model="model.remittanceAmount"
                    placeholder="请输入保证金金额"
                    style="width: 100%"
                    @change="countCurrentAmount"
                  />
                  <a-input-number
                    :hidden="true"
                    :disabled="formDisabled || showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    v-model="model.unreturnAmount"
                    placeholder="请输入归还金额"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="本次支付金额"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="currentPaymentAmount"
                >
                  <a-input-number
                    :disabled="formDisabled || showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    v-model="model.currentPaymentAmount"
                    placeholder="请输入本次支付金额"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="收款单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payeeUnit">
                  <!-- <select-customer-input  :disabled="formDisabled" v-model="model.payeeUnit" commercialType="1" @back="getCustomerInfo" /> -->
                  <a-input
                    :disabled="formDisabled || showFlowSubmitButton"
                    v-model="model.payeeUnit"
                    placeholder="请输入收款单位名称"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.remittanceMode=='2'">
                <a-form-model-item
                  label="收款方银联号"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="cnapsCode"
                >
                 <SelectFinanceKingdeeBankInput :disabled="formDisabled || showFlowSubmitButton"  v-model="model.cnapsCode"  placeholder="请选择收款方银联号"  @back="backCnapsCode" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.remittanceMode=='2'">
                <a-form-model-item
                  label="收款方开户行"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="payeeBankName"
                >
                  <a-input
                    :disabled="formDisabled || showFlowSubmitButton"
                    v-model="model.payeeBankName"
                    placeholder="请输入收款方开户行" disabled
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.remittanceMode=='2'">
                <a-form-model-item
                  label="收款方账户"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="payeeAccountNo"
                >
                  <a-input
                    :disabled="formDisabled || showFlowSubmitButton"
                    v-model="model.payeeAccountNo"
                    :max-length="32"
                    placeholder="请输入收款方账户"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.remittanceMode=='2'">
                <a-form-model-item
                  label="收款联系人"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="payeeContacts"
                >
                  <a-input
                    :disabled="formDisabled || showFlowSubmitButton"
                    v-model="model.payeeContacts"
                    placeholder="请输入收款联系人"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.remittanceMode=='2'">
                <a-form-model-item
                  label="收款联系方式"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="payeeContactInfo"
                >
                  <a-input
                    :disabled="formDisabled || showFlowSubmitButton"
                    v-model="model.payeeContactInfo"
                    placeholder="请输入收款联系方式"
                    type="number"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="预计返款时间"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="estimateRefundTime"
                >
                  <j-date
                    :disabled="formDisabled || showFlowSubmitButton"
                    placeholder="请选择预计返款时间"
                    v-model="model.estimateRefundTime"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="打款附言" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postscript">
                  <a-textarea :disabled="formDisabled && !showFlowSubmitButton" v-model="model.postscript" rows="4" placeholder="请输入打款附言" :max-length="200"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="情况说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled || showFlowSubmitButton" v-model="model.remark" rows="4" placeholder="请输入情况说明" :max-length="500"/>
                </a-form-model-item>
              </a-col>
              <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                <a-button @click="submitForm">提 交</a-button>
              </a-col> -->
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="3" header="附件信息">
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachments">
                  <j-upload :disabled="formDisabled || showFlowSubmitButton" v-model="model.attachments"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
    </j-form-container>
    <a-row type="flex" justify="center">
      <a-col :span="24">
        <!-- 流程历史跟踪 -->
        <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData"></common-flow-list>
      </a-col>
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }" @click="onPrint">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <!-- 项目详情开始 -->
    <erp-project-report-modal ref="projectReport"></erp-project-report-modal>
    <!-- 项目详情结束 -->
    <ErpProjectTenderPenetrate ref="projectTender" ></ErpProjectTenderPenetrate>
    <ErpFinanceProjectDepositBillFormPrint v-if="formBpm" class="print_style" id="depositContent" :formData="model" :formDataHistory="formData"></ErpFinanceProjectDepositBillFormPrint>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import moment from 'moment'
import { mapGetters } from 'vuex'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
import CommonFlowList from '@/views/components/CommonFlowList.vue';
import SelectOmcProjectTenderInputForDepositBill from '@/views/scy/components/SelectOmcProjectTenderInputForDepositBill';
import ErpProjectTenderPenetrate from '@/views/scy/penetrate/ErpProjectTenderPenetrate'
import Print from '@/utils/print/printarea.js';
import ErpFinanceProjectDepositBillFormPrint from './ErpFinanceProjectDepositBillFormPrint' // 打印
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import SelectFinanceKingdeeBankInput from '@/views/scy/components/SelectFinanceKingdeeBankInput'

export default {
  name: 'ErpFinanceProjectDepositBillForm',
  components: {
    SelectFinanceKingdeeBankInput,
    SelectCompanyInput,
    SelectPersonInput,
    SelectTeamInput,
    SelectCustomerInput,
    SelectProjectInput,
    SelectUserByDep,
    CommonFlowList,
    SelectOmcProjectTenderInputForDepositBill,
    ErpProjectTenderPenetrate,
    ErpFinanceProjectDepositBillFormPrint,
    SelectFinanceDepositBillInput: () => import('@/views/scy/components/SelectFinanceDepositBillInput'),
    ErpProjectReportModal: () => import('@/views/erp/report/project/modules/ErpProjectReportModal')
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    activeKey(key) {
      this.$forceUpdate()
    },
    // 监听费用类型变动，变动会更新时间戳
    'model.feeCategory': {
      handler() {
        this.timer = new Date().getTime()
      },
      deep: true,
      immediate: true
    },
    // 监听申请公司变动，变动会更新时间戳
    'model.applicant': {
      handler() {
        this.timer = new Date().getTime()
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      model: {},
      // 时间戳 用于更新关联项目的key，用于更新dom 2022-11-21
      timer: '',
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      activeKey: ['1', '2', '3'],
      confirmLoading: false,
      validatorRules: {
        bpmBizTitle: [
              { required: true, message: '请输入流程标题!' }
          ],
        applicant: [
            { required: true, message: '请输入申请公司!' }
        ],
        billCategory: [
            { required: true, message: '请选择单据类型!' }
        ],
        tenderId: [
            { required: true, message: '请选择关联投标档案!' }
        ],
        preProjectId: [
            { required: true, message: '请选择关联项目!' }
        ],
        remittanceUnit: [
            { required: true, message: '请选择汇款单位!' }
        ],
        remittanceEndTime: [
            { required: true, message: '请选择汇款截止时间!' }
        ],
        feeCategory: [
            { required: true, message: '请选择费用类型!' }
        ],
        remittanceAccountRequirements: [
            { required: true, message: '请输入汇款账户要求!' }
        ],
        remittanceMode: [
            { required: true, message: '请输入汇款方式!' }
        ],
        remittanceAmount: [
          { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保证金金额!' }
        ],
        payeeUnit: [
            { required: true, message: '请输入收款单位名称!' }
        ],
        cnapsCode: [
            { required: true, message: '请选择收款方开户行联行号!' }
        ],
        payeeBankName: [
            { required: true, message: '请输入收款方开户行!' }
        ],
        isRelDeposit: [
            { required: true, message: '请选择是否投标转履约!' }
        ],
        relDepositBill: [
            { required: true, message: '请选择关联的投标保证金!' }
        ],
        letterAmount: [
          { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保函金额!' }
        ],
        payeeAccountNo: [
            { required: true, message: '请输入银行账户!' }
            // { pattern: /^[1-9]{1}\d{7,29}$/, message: '请输入正确的银行账户!' },
        ],
        remark: [
            { required: true, message: '请输入情况说明!' }
            // { pattern: /^[1-9]{1}\d{7,29}$/, message: '请输入正确的银行账户!' },
        ],
        payeeContactInfo: [
            { required: false, message: '请输入联系方式!' },
            { pattern: /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/, message: '请输入正确的联系方式!' }
        ]
      },
      url: {
        queryProjectReportById: '/api/erp/project/report/erpProjectReport/queryById',
        queryTenderId: '/api/erp/omc/tenderresult/erpOmcTenderResult/queryResultByTenderId',
        queryTenderResultDataByProjectId: '/api/erp/omc/tender/erpOmcTenderResultFinal/queryTenderResultDataByProjectId',
        queryTenderDataByProjectId: '/api/erp/omc/tender/erpOmcTenderFinal/queryTenderDataByProjectId',
        getBillNo: '/api/erp/finance/deposit/erpFinanceProjectDepositBill/getBillNo',
        add: '/api/erp/finance/deposit/erpFinanceProjectDepositBill/add',
        edit: '/api/erp/finance/deposit/erpFinanceProjectDepositBill/edit',
        queryById: '/api/erp/finance/deposit/erpFinanceProjectDepositBill/queryById',
        // 查询收款单位信息
        queryPaymentById: '/api/erp/purchase/supplier/erpPurchaseSupplier/queryById',
        queryTenderById: '/api/erp/omc/tender/erpOmcTenderFinal/queryById',
        // 查询项目信息
        queryOmcProjectById: 'api/erp/omc/project/erpOmcProject/queryById'
      },
      dictOptions: []
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    // 如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
    // 字典
    this.initDicts()
  },
  methods: {
    backCnapsCode(data){
      if(data){
        this.$set(this.model, 'payeeBankName', data[0].bankBranchName)
      }
    },
    goProjectDetail(record) {
      const that = this
      this.$nextTick(() => {
        getAction(this.url.queryProjectReportById, { id: record.preProjectId }).then((res) => {
          if (res.success) {
            const model = res.result
            that.$refs.projectReport.getDetail(model);
            that.$refs.projectReport.title = '项目一览表';
          }
        })
      })
    },
    goTenderDetail(record) {
      this.$refs.projectTender.initData(record.tenderId);
      this.$refs.projectTender.visible = true;
    },
    countCurrentAmount(){
      console.log(this.model.remittanceAmount)
      console.log(this.model.originPaymentAmount)
      if(this.model.remittanceAmount && this.model.originPaymentAmount ){
        let amount=(this.model.remittanceAmount-this.model.originPaymentAmount).toFixed(2)
        if(amount>0){
          this.$set(this.model, 'currentPaymentAmount', amount);
        }else{
          this.$set(this.model, 'currentPaymentAmount', 0);
        }
      }else if(this.model.remittanceAmount){
        this.$set(this.model, 'currentPaymentAmount', this.model.remittanceAmount);
      }else{
        this.$set(this.model, 'currentPaymentAmount', 0);
      }
    },
    popupCallback(data) {
      if (((this.model.feeCategory == '1' || this.model.feeCategory == '3')  && data) || ((this.model.feeCategory == '2' || this.model.feeCategory == '4' || this.model.feeCategory == '5'  )&& data.choseObj == undefined)) {
        if (data.id) {
          this.$set(this.model, 'preProjectId', data.prepareProjectId);
          // this.$set(this.model, 'remittanceAmount', data.tenderBond);
          this.$set(this.model, 'projectUndertaker', data.undertaker)
          this.$set(this.model, 'teamId', data.businessTeam)
          if (this.model.feeCategory == '2'  || this.model.feeCategory == '4' || this.model.feeCategory == '5' ) {
            getAction(this.url.queryTenderId, { tenderId: data.id })
            .then(res => {
              let resa = res.result
              if (resa) {
                this.$set(this.model, 'remittanceAmount', res.result.bail);
              } else {
                this.$set(this.model, 'remittanceAmount', 0);
              }
              this.countCurrentAmount();
            }).catch(e => {
              console.log(e)
            })
          } else {
            this.$set(this.model, 'remittanceAmount', data.tenderBond);
            this.countCurrentAmount();
          }
          if (this.model.feeCategory == '1'  || this.model.feeCategory == '3' ) {
            getAction(this.url.queryOmcProjectById, { id: data.prepareProjectId })
            .then(resss => {
              this.$set(this.model, 'attributionYear', resss.result.year);
              this.$set(this.model, 'projectCode', resss.result.finalNo);
            }).catch(e => {
              console.log(e)
            })
          } else {
            this.$set(this.model, 'remittanceAmount', data.tenderBond);
            this.countCurrentAmount();
          }
        } else if (this.model.feeCategory == '1'  || this.model.feeCategory == '3' ) {
          this.$set(this.model, 'preProjectId', null);
          this.$set(this.model, 'remittanceAmount', 0);
          this.$set(this.model, 'projectUndertaker', null)
          this.$set(this.model, 'teamId', null)
          this.countCurrentAmount();
        }
      } else if (this.model.feeCategory == '2' || this.model.feeCategory == '4' || this.model.feeCategory == '5' ) {
        getAction(this.url.queryTenderResultDataByProjectId, { projectId: this.model.preProjectId }).then((res) => {
          if (res.success) {
            let data = res.result.records || res.result
            // 保证金金额
            this.$set(this.model, 'remittanceAmount', data.bail);
            // 收款单位名称
            this.radioModeChange(this.model.remittanceMode)
            this.countCurrentAmount();
          }
        })
        getAction(this.url.queryOmcProjectById, { id: this.model.preProjectId }).then((res) => {
          if (res.success) {
            let data = res.result.records || res.result
            this.$set(this.model, 'projectUndertaker', data.undertaker)
            this.$set(this.model, 'teamId', data.teamName)
          }
        })
      } else {
        this.$set(this.model, 'preProjectId', null);
        this.$set(this.model, 'remittanceAmount', 0);
        this.$set(this.model, 'projectUndertaker', null)
        this.$set(this.model, 'teamId', null)
        this.countCurrentAmount();
      }
    },
    // 切换方式
    radioModeChange(data) {
      if (data) {
        // 选择转账时带出收款单位中 收款方开户行、收款方账户
        if (data == 2 && this.model && this.model.payeeUnit) {

          // 迭代11 ： 单据中打款信息-收款单位名称、收款方开户行、收款方账户字段改为非带入，人工填写。
          // getAction(this.url.queryPaymentById,{id:this.model.payeeUnit})
          // .then(res=>{
          //   if(res&&res.result){
          //     this.$set(this.model,'payeeAccountNo',res.result.bankAccountNo)
          //     this.$set(this.model,'payeeBankName',res.result.bankName)
          //   }
          // }).catch(e=>{
          //   console.log(e)
          // })
        } else {
          this.$set(this.model, 'payeeBankName', null);
          this.$set(this.model, 'cnapsCode', null);
          this.$set(this.model, 'payeeAccountNo', null);
          this.$set(this.model, 'payeeContacts', '');
          this.$set(this.model, 'payeeContactInfo', '');
        }
      }
    },
    // 修改按钮
    radioChange (item) {
      this.$set(this.model, 'preProjectId', null);
      this.$set(this.model, 'tenderId', null);
      this.$set(this.model, 'projectUndertaker', null);
      this.$set(this.model, 'teamId', null);
      this.$set(this.model, 'attributionYear', null);
      this.$set(this.model,'projectCode',null);
      this.$set(this.model, 'remittanceAmount', 0);
      // 由项目自动带出的信息也清空（收款单位名称、收款方开户行、收款方账户）
      this.$set(this.model, 'payeeUnit', null)
      this.$set(this.model, 'payeeAccountNo', null)
      this.$set(this.model, 'cnapsCode', null);
      this.$set(this.model, 'payeeBankName', null)
      if (this.model.feeCategory == '1' || this.model.feeCategory == '3' ) {
        this.$set(this.validatorRules, 'tenderId', [{ required: true, message: '请选择关联投标档案!' }]);
      } else if (this.model.feeCategory == '2' || this.model.feeCategory == '4' || this.model.feeCategory == '5') {
        this.$set(this.validatorRules, 'tenderId', [{ required: false, message: '请选择关联投标档案!' }]);
      }
      if(this.model.feeCategory=='1' || this.model.feeCategory == '2' ){
        this.$set(this.validatorRules,'remittanceAmount',[
          { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保证金金额!' }
        ])
      }else{
        this.$set(this.validatorRules,'remittanceAmount',[
          { required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保证金金额!' }
        ])
      }
      if(this.model.isRelDeposit){
        this.$set(this.model, 'isRelDeposit', null)
        this.$set(this.model, 'relDepositBill', null)
        this.$set(this.model, 'originPaymentAmount', null)
      }
    },
    // 关联项目
    selectProject(data) {
      if (data) {
        if (data.choseObj[0]) {
          this.$set(this.model, 'tenderId', '');
          this.model.projectCode = data.choseObj[0].finalNo
          this.model.attributionYear = data.choseObj[0].year
          this.$set(this.model, 'projectUndertaker', data.choseObj[0].undertaker)
          this.$set(this.model, 'teamId', data.choseObj[0].teamName)
          // 投标保证金
          if (this.model.feeCategory == '1'|| this.model.feeCategory == '3' ) {
            getAction(this.url.queryTenderDataByProjectId, { projectId: data.choseObj[0].id }).then((res) => {
              if (res.success) {
                let data = res.result.records || res.result
                if (data.length > 0) {
                  // 保证金金额
                  this.$set(this.model, 'remittanceAmount', data[0].tenderBond);
                  // 收款单位名称
                  // 迭代11 单据中打款信息-收款单位名称、收款方开户行、收款方账户字段改为非带入，人工填写。
                  // this.$set(this.model, 'payeeUnit', data[0].ownerUnit)
                  // 如果带出了收款单位名称且汇款方式为转账且收款信息没有就带出(此操作为了防止单子填完修改费用类型时收款信息带不出)
                  // if(this.model.payeeUnit&&this.model.remittanceMode
                  //   &&this.model.remittanceMode==2
                  //   &&!this.model.payeeBankName&&!this.model.payeeAccountNo){
                  //     // 调用切换为转账的方法主动触发反写收款信息
                  //     this.radioModeChange(this.model.remittanceMode)
                  // }
                  // 调用切换为转账的方法主动触发反写收款信息
                  this.radioModeChange(this.model.remittanceMode)
                  this.countCurrentAmount();
                } else {
                  this.$set(this.model, 'remittanceAmount', 0);
                  this.countCurrentAmount();
                }
              }
            })
          // 履约保证金
          } else if (this.model.feeCategory == '2'|| this.model.feeCategory == '4' || this.model.feeCategory == '5' ) {
            if(this.model.relDepositBill){
              this.$set(this.model, 'isRelDeposit', null)
              this.$set(this.model, 'relDepositBill', null)
              this.$set(this.model, 'originPaymentAmount', null)
            }
            getAction(this.url.queryTenderResultDataByProjectId, { projectId: data.choseObj[0].id }).then((res) => {
              if (res.success) {
                let data = res.result.records || res.result
                // 保证金金额
                this.$set(this.model, 'remittanceAmount', data.bail);
                // 收款单位名称
                //   this.$set(this.model, 'payeeUnit', data.ownerUnit);
                  // 如果带出了收款单位名称且汇款方式为转账且收款信息没有就带出(此操作为了防止单子填完修改费用类型时收款信息带不出)
                  // if(this.model.payeeUnit&&this.model.remittanceMode
                  //   &&this.model.remittanceMode==2
                  //   &&!this.model.payeeBankName&&!this.model.payeeAccountNo){
                  //     // 调用切换为转账的方法主动触发反写收款信息
                  //     this.radioModeChange(this.model.remittanceMode)
                  // }
                  // 调用切换为转账的方法主动触发反写收款信息
                  this.radioModeChange(this.model.remittanceMode)
                  this.countCurrentAmount();
              }
            })
          }
        } else {
          this.$set(this.model, 'projectUndertaker', null)
          this.model.projectCode = null
          this.model.attributionYear = null
          this.$set(this.model, 'teamId', null)
          this.$set(this.model, 'isRelDeposit', null)
          this.$set(this.model, 'relDepositBill', null)
          this.$set(this.model, 'originPaymentAmount', null)
          this.$set(this.model, 'remittanceAmount', 0);
          this.$set(this.model, 'tenderId', null);
          this.countCurrentAmount();
        }
      }else{
        this.countCurrentAmount();
      }

    },
    // 获取收款单位信息
    getCustomerInfo(data) {
      if (data) {
        this.$set(this.model, 'payeeAccountNo', data[0].bankAccountNo)
        this.$set(this.model, 'payeeBankName', data[0].bankName)
      }
    },
    // 获取编号
    getBillNo() {
      getAction(this.url.getBillNo, {}).then((res) => {
        if (res.success) {
          let data = res.result.records || res.result
          this.$set(this.model, 'billNo', data);
          this.$set(this.model, 'bpmBizTitle', this.userInfo().employee.name + '-' + data);
        }
      })
    },
    ...mapGetters(['userInfo']),
    add() {
      this.edit(this.modelDefault);
      this.getBillNo();
      this.model.proposer = this.userInfo().empId;
      this.model.proposerDeptId = this.userInfo().deptId;
      this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss');
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      // 加载关联的投标
      if (this.formDisabled && this.model.tenderId) {
        getAction(this.url.queryTenderById, { id: this.model.tenderId }).then(res => {
          this.$set(this.model, 'tenderNo', res.result.finalNo);
        }).catch(e => {
          console.log(e)
        })
      }
      if (this.model.feeCategory == '1' || this.model.feeCategory == '3' ) {
        this.$set(this.validatorRules, 'tenderId', [{ required: true, message: '请选择关联投标档案!' }]);

      } else if (this.model.feeCategory == '2'|| this.model.feeCategory == '4' || this.model.feeCategory == '5' ) {
        this.$set(this.validatorRules, 'tenderId', [{ required: false, message: '请选择关联投标档案!' }]);
      }
      if(this.model.feeCategory=='1' || this.model.feeCategory == '2' ){
        this.$set(this.validatorRules,'remittanceAmount',[
          { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保证金金额!' }
        ])
      }else{
        this.$set(this.validatorRules,'remittanceAmount',[
          { required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入保证金金额!' }
        ])
      }
    },
    // 渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    relBillData(data) {
      if(data[0]) { // 保证金
          let remittanceMode = data[0].remittanceMode;// 汇款方式
          let remittanceAmount = data[0].remittanceAmount// 归还总额
          let payeeBankName = data[0].payeeBankName;// 银行金额
          let payeeAccountNo = data[0].payeeAccountNo;// 账号
          let payeeUnit = data[0].payeeUnit;// 收款单位
          let cnapsCode = data[0].cnapsCode;// 联行号
          let payeeContactInfo = data[0].payeeContactInfo;// 联行号
          let payeeContacts = data[0].payeeContacts;// 联行号
          this.$set(this.model, 'remittanceMode', remittanceMode);
          this.$set(this.model, 'originPaymentAmount', remittanceAmount);
          this.$set(this.model, 'payeeUnit', payeeUnit);
          this.$set(this.model, 'payeeAccountNo', payeeAccountNo);
          this.$set(this.model, 'payeeBankName', payeeBankName);
          this.$set(this.model, 'cnapsCode', cnapsCode);
          this.$set(this.model, 'payeeContactInfo', payeeContactInfo);
          this.$set(this.model, 'payeeContacts', payeeContacts);
          this.countCurrentAmount();
      }
    },
    isRelDeposit(){
      if(this.model.isRelDeposit==='0'){
        this.$set(this.model, 'relDepositBill', null);
        this.$set(this.model, 'originPaymentAmount', null);
      }
    },
    submitForm(type) {
      const that = this;
      if (type == 'draft') {
        // 代表保存草稿不做校验
        this.submit(type)
      } else if (type == 'submit') {
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.submit(type)
          } else {
            that.$emit('unButtonDisable')
          }
        })
      } else {
        console.log('传参数，保存：draft，提交：submit')
      }
    },
    submit (type) {
      const that = this;
      let teamName = filterMultiDictText(this.dictOptions['teamManager'], that.model.teamId + '')
        let createTime = this.getCreateTime()
        // 迭代16.7 取保证金所属团队-申请人-申请日期（年月日）-金额（取子表中保证金金额）
        that.model.bpmBizTitle = (that.model.teamId ? teamName + ' - ' : '') + that.userInfo().empName + ' - ' + createTime + (that.model.remittanceAmount ? ' - ' + that.model.remittanceAmount : '') // 标题
      that.confirmLoading = true;
      let httpurl = '';
      let method = '';
      if (!this.model.id) {
        httpurl += this.url.add;
        method = 'post';
      } else {
        httpurl += this.url.edit;
        method = 'put';
      }
      this.$set(this.model, 'unreturnAmount', this.model.remittanceAmount);
      httpAction(httpurl, this.model, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          if (method == 'post') {
            that.model.id = res.result;
          }
          that.$emit('ok', type, that.model);
        } else {
          that.$message.warning(res.message);
          that.$emit('unButtonDisable');
        }
      }).catch((error) => {
        that.$emit('unButtonDisable', error);
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    applicantChange(data) {
      this.radioChange();
      this.$set(this.model, 'remittanceUnit', this.model.applicant);
    },

    // 点击打印
    onPrint () {
        let print = new Print({
          el: '#depositContent',
          popTitle: '保证金申请详情 · 禾美环保',
          endCallback() {

          }
        })
      },
    initDicts() {
      initDictOptions('erp_omc_team_manager,alias_name,id').then((res) => {
        if (res.success) {
          this.$set(this.dictOptions, 'teamManager', res.result)
        }
      })
    },
    getCreateTime() {
      let createTime = this.model.createTime;
      let date = createTime.substring(0, 10)
      return date.substring(0, 4) + '年' + date.substring(5, 7) + '月' + date.substring(8, 10) + '日';
    }
  }
}
</script>
<style>
  @import "../../../../../utils/print.css";
  .print_style{
  position: absolute;
  top: 0;
  z-index: -1111;
}
</style>
